package top.lc951.myandroid.views.card;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.example.indicatorlib.utils.Utils;

import top.lc951.myandroid.R;
import top.lc951.myandroid.model.CardOverlayModel;

public class CardOverlayAdapter extends BaseOverlayPageAdapter<CardOverlayModel> {
    private LayoutInflater mInflater;

    public CardOverlayAdapter(Context context) {
        super(context);
        mInflater = LayoutInflater.from(context);
    }

    int gravity = Gravity.CENTER_VERTICAL;

    @Override
    protected View itemView(int position) {
        return mInflater.inflate(R.layout.live_banner_viewpager_item, null);


    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, final int position) {
        final CardOverlayModel itemObj = getList().get(position);
        View rootView = itemView(position);
        if (null == rootView) {
            throw new RuntimeException("you should set a item layout");
        }
        ImageView imageView = rootView.findViewById(R.id.card_iv);
        if (null == imageView) {
            throw new RuntimeException("you should set a item layout");
        }
        LinearLayout ll_content = rootView.findViewById(R.id.ll_content);
        CardView cardView = rootView.findViewById(R.id.card_view);
        if(1==getCount()){
            LinearLayout.LayoutParams layoutParams
                    =new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                    , Utils.dp2px(getContext(),157));
            layoutParams.rightMargin=Utils.dp2px(getContext(),10);
            layoutParams.leftMargin=Utils.dp2px(getContext(),10);
            cardView.setLayoutParams(layoutParams);
        }
        if (!TextUtils.isEmpty(itemObj.getCoverUrl())) {
            Glide.with(getContext())
                    .load(itemObj.getCoverUrl())
                    .error(R.mipmap.ic_img04)
                    .into(imageView);
        }
        container.addView(rootView);

        return rootView;
    }
}
